package com.ysten.basic.web.basic;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.ysten.basic.common.helper.JmsHelper;
import com.ysten.basic.common.redis.JedisTemplate;
import com.ysten.basic.common.redis.JedisUtils;
import com.ysten.basic.common.utils.DateProvider;
import com.ysten.basic.common.utils.SystemConfigLoader;
import com.ysten.basic.common.utils.constant.Constant;
import com.ysten.basic.service.system.AccountService;
import com.ysten.basic.service.system.ButtonService;
import com.ysten.basic.service.system.RoleManageService;
import com.ysten.basic.service.system.ShiroDbRealm.ShiroUser;
import com.ysten.basic.service.system.SysMenuService;
import com.ysten.basic.service.system.SystemConfigService;
import com.ysten.basic.service.system.SystemDicService;
import com.ysten.basic.service.system.UserService;

/**
 * <业务-基础Controller>
 * 
 * @author liwei
 * @version [版本号, 2015年7月16日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public abstract class BusinessBasicController
{
    
    @Autowired
    protected JmsHelper jmsHelper;
    
    @Autowired
    protected RoleManageService roleService;
    
    @Autowired
    protected SystemDicService systemDicService;
    
    @Autowired
    protected SystemConfigService systemConfigService;
    
    @Autowired
    protected SystemConfigLoader systemConfigLoader;
    
    @Autowired
    protected ButtonService buttonService;
    
    @Autowired
    protected SysMenuService sysMenuService;
    
    @Autowired
    protected AccountService accountService;
    
    @Autowired
    protected UserService userService;
 
    
    private JedisTemplate jedisTemplate;
    
    protected DateProvider dateProvider = DateProvider.DEFAULT;
    
    protected SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    
    protected SimpleDateFormat dfPart = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    
    protected SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 实现前台日期类型的转换
     * 
     * @param binder
     * @param request
     */
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request)
    {
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        
        /**
         * 自动转换日期类型的字段格
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
    
    /**
     * 取出Shiro中的当前用户.
     */
    public ShiroUser getCurrentUser()
    {
        ShiroUser user = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
        return user;
    }
    
    /**
     * <redis相关>
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    public JedisTemplate getJedisTemplate()
    {
        if (jedisTemplate != null)
        {
            return jedisTemplate;
        }
        String redisHost = systemConfigLoader.getStringValue(Constant.REDIS_URL);
        int redisPort = Integer.parseInt(systemConfigLoader.getStringValue(Constant.REDIS_PORT));
        int maxActive = Integer.parseInt(systemConfigLoader.getStringValue(Constant.REDIS_MAXACTIVE));
        int maxIdle = Integer.parseInt(systemConfigLoader.getStringValue(Constant.REDIS_MAXIDLE));
        
        JedisPoolConfig poolConfig = JedisUtils.createPoolConfig(maxIdle, maxActive);
        JedisPool pool = new JedisPool(poolConfig, redisHost, redisPort);
        // 测试是否连接上了
        pool.returnResource(pool.getResource());
        jedisTemplate = new JedisTemplate(pool);
        return jedisTemplate;
    }
}
